package com.fulihui.wgj.dal.mybatis.repository;

import com.fulihui.wgj.dal.mybatis.domain.WxParamQrcodeInfo;
import com.fulihui.wgj.dal.mybatis.mapper.WxParamQrcodeInfoMapper;
import com.fulihui.wgj.facade.request.qrcode.WxQrcodePageQueryRequest;
import com.fulihui.wgj.facade.request.qrcode.WxQrcodeQueryRequest;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wang_wx
 * @date 2018-06-20
 */
@Component
public class WxParamQrcodeInfoRepository extends BaseRepository<WxParamQrcodeInfo> {

    @Resource
    WxParamQrcodeInfoMapper wxParamQrcodeInfoMapper;

    /**
     * 查询
     *
     * @param request
     * @return
     */
    public List<WxParamQrcodeInfo> queryListByRequest(WxQrcodeQueryRequest request) {
        Example example = buildExample(request);
        List<WxParamQrcodeInfo> wxParamQrcodeInfos = wxParamQrcodeInfoMapper.selectByExample(example);
        return wxParamQrcodeInfos;
    }

    /**
     * 分页查询
     *
     * @param request
     * @return
     */
    public PageInfo<WxParamQrcodeInfo> queryPageListByRequest(WxQrcodePageQueryRequest request) {
        Example example = buildExample(request);
        PageHelper.startPage(request.getPage(), request.getRows());
        List<WxParamQrcodeInfo> wxParamQrcodeInfos = wxParamQrcodeInfoMapper.selectByExample(example);
        return new PageInfo<>(wxParamQrcodeInfos);
    }

    Example buildExample(WxQrcodeQueryRequest request) {
        Example example = new Example(WxParamQrcodeInfo.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(request.getAppId())) {
            criteria.andEqualTo("appId", request.getAppId());
        }
        if (StringUtils.isNotBlank(request.getSceneDescribe())) {
            criteria.andLike("sceneDescribe", "%" + request.getSceneDescribe() + "%");
        }
        example.orderBy("gmtCreate").desc();
        return example;
    }

    Example buildExample(WxQrcodePageQueryRequest request) {
        Example example = new Example(WxParamQrcodeInfo.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(request.getAppId())) {
            criteria.andEqualTo("appId", request.getAppId());
        }
        if (StringUtils.isNotBlank(request.getSceneDescribe())) {
            criteria.andLike("sceneDescribe", "%" + request.getSceneDescribe() + "%");
        }
        example.orderBy("gmtCreate").desc();
        return example;
    }
}
